﻿using System;

class MaxSumOfKElements
{
    static void Main()
    {
        // get the length of the array
        Console.Write("Enter length of the array : ");
        string length = Console.ReadLine();
        int lengthOfArray;
        while (!int.TryParse(length, out lengthOfArray))
        {
            Console.Write("Invalid input. Enter again length for the array : ");
            length = Console.ReadLine();
        }

        // get number for the elements of the sum
        Console.Write("Enter number of elements for the sum : ");
        string kElementsStr = Console.ReadLine();
        int kElements;
        while (!int.TryParse(kElementsStr, out kElements))
        {
            Console.Write("Invalid input. Enter again the number of elements for the sum : ");
            length = Console.ReadLine();
        }

        if (lengthOfArray <= 0)
        {
            Console.WriteLine("Inavlid input for array's length. Start the program again.");
        }
        else if (kElements > lengthOfArray)
        {
            Console.WriteLine("Invalid input for number of elements for the sum.");
            Console.WriteLine("These elements should be less or equal to the length of the array.");
            Console.WriteLine("Start the program again.");
        }
        else
        {
            decimal sumKElements = 0;

            decimal[] array = new decimal[lengthOfArray];

            for (int index = 0; index < lengthOfArray; index++)
            {
                // get the element of the array
                Console.Write("Enter element of the array : ");
                string element = Console.ReadLine();
                decimal elementOfArray;
                while (!decimal.TryParse(element, out elementOfArray))
                {
                    Console.Write("Invalid input. Enter element again : ");
                    element = Console.ReadLine();
                }
                array[index] = elementOfArray;
            }


            int firstIndexOfKElements = 0; // the index where starts the sequence with max sum
            int nextKElements = kElements; // this is the upper limit for the cycle for the sum of the sequence
            int startIndex = 0; // this is the index from which starts the cycle for the sequence
            decimal maxSum = 0; // this is the max sum that is found

            while (nextKElements <= lengthOfArray)
            {

                for (int index = startIndex; index < nextKElements; index++)
                {
                    sumKElements += array[index];
                }
                if (sumKElements > maxSum)
                {
                    maxSum = sumKElements;
                    firstIndexOfKElements = startIndex;
                }
                nextKElements++;
                startIndex++;
                sumKElements = 0;
            }
            Console.WriteLine("Max sum = " + maxSum);

            // print the sequence with maximal sum
            string resultString = "";
            for (int i = firstIndexOfKElements; i < firstIndexOfKElements + kElements; i++)
            {
                // for the last element of the sequence don't put comma after the element
                if (i == firstIndexOfKElements + kElements - 1)
                {
                    resultString = resultString + array[i];
                }
                else
                {
                    resultString = resultString + array[i] + ", ";
                }
            }
            Console.WriteLine("And the sequence with max sum is : {" + resultString + "}");
        }
    }
}
